package com.easysoft.framework.action;

import com.easysoft.commons.utils.FileUtils;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.trademark.model.SysTrademark;
import com.easysoft.framework.trademark.model.TrademarkQuery;
import com.easysoft.framework.trademark.model.TrademarkView;
import com.easysoft.framework.trademark.service.TrademarkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/9/12.
 * 商标模块
 */
@RestController
@RequestMapping(value = SystemTrademarkAction.BASE_VIEW)
public class SystemTrademarkAction {


    public static final String BASE_VIEW = "/trademark";
    private final static Logger logger = LoggerFactory.getLogger(SystemTrademarkAction.class);

    @Autowired
    @Qualifier("service.TrademarkService")
    private TrademarkService trademarkService;


    @RequestMapping(value = "/listTrademark", method = RequestMethod.GET)
    public ModelAndView listTrademark(Page page, TrademarkQuery query, HttpSession session) {
        logger.info("Call SystemTrademarkAction.listTrademark({},{})", page.toString(), query.toString());
        List<TrademarkView> listTrademark = trademarkService.selectSysTrademarkByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listTrademark");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listTrademark", listTrademark);
        return mv;
    }

    @RequestMapping(value = "/editTrademark", method = RequestMethod.GET)
    public ModelAndView editTrademark(String action, @RequestParam(required = false) String id) {
        logger.info("Call SystemTrademarkAction.editTrademark({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editTrademark");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysTrademark entity = trademarkService.selectByPrimaryKey(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveTrademark", method = RequestMethod.POST)
    public int saveTrademark(String action, SysTrademark trademark, @RequestParam(value = "multipartFile", required = false) MultipartFile multipartFile, HttpServletRequest request, HttpSession session) {
        logger.info("Call SystemTrademarkAction.saveTrademark({},{})", action, trademark.toString());
        if(multipartFile != null && !("").equals(multipartFile)){
            trademark.setBrandLogo( FileUtils.savePicture(multipartFile,"trademark",request));
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(trademark.getId())) {
                return 0;
            }
            return trademarkService.addSysTrademark(trademark);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(trademark.getId())) {
                return 0;
            }
            return trademarkService.updateSysTrademark(trademark);
        }
        return 0;
    }


    @RequestMapping(value = "/updateSysTrademark", method = RequestMethod.POST)
    @SystemControllerLog(description = "修改商标信息")
    public int updateSysTrademark(SysTrademark trademark, HttpSession session) {
        logger.info("Call SystemTrademarkAction.updateSysTrademark({})", trademark.toString());
        return trademarkService.updateSysTrademark(trademark);
    }

    @RequestMapping(value = "/deleteTrademark", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除商标信息")
    public int deleteSysTrademark(String id) {
        logger.info("Call SystemTrademarkAction.deleteSysTrademark({})", id);
        return trademarkService.deleteSysTrademark(id);
    }
}
